package com.qf.day03;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

class ShareDate{

    private AtomicInteger atomicInteger = new AtomicInteger(0);
    //自旋锁 ==  CAS +   do while
    //乐观锁 == 自旋锁（圈数）
    //悲观锁 ==
    public int incr() throws InterruptedException {

        int value = atomicInteger.get();
        //取
        //TimeUnit.MILLISECONDS.sleep(100);

        return atomicInteger.addAndGet(1);
        // +
    }

}
/**
 * 类加载器 ：
 *   1、引导类加载器   启动类加载器
 *   2、扩展类加载器
 *   3、应用类加载器
 *
 *   4、自定义类加载器
 * @author lixu
 */
public class UnSaleTest {



    public static void main(String[] args) {
        ShareDate dataOne = new ShareDate();

        for (int i = 0; i < 1000; i++) {
            new Thread(() -> {
                try {
                    System.out.println(dataOne.incr());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }).start();
        }



        AtomicInteger atomicInteger = new AtomicInteger(10);

        // update  set  version= 11A where version= 10A
        // update  set  version= 12A where version= 11A
        // update  set  version= 10B where version= 12A //同事
        // update  set  version= 11B where version= 10B //同事

        atomicInteger.compareAndSet(10,11 );
        atomicInteger.compareAndSet(11,12 );
        atomicInteger.compareAndSet(12,13 );
        atomicInteger.compareAndSet(13,14 );
        //git
        System.out.println(atomicInteger.get());
    }
}
